<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/taglib.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>添加</title>
	<%@ include file="/WEB-INF/jsp/include.jsp"%>
	<%@ include file="/WEB-INF/jsp/loading.jsp" %>
	<script type="text/javascript" src="${path}/resource/js/system/declareparameter/add.js"></script>
	<script type="text/javascript" src="${path}/resource/js/system/declareparameter/common.js"></script>
	<script type="text/javascript"  src="${path}/resource/js/jquery/jquery-easyui-1.4.5/extends/jquery.layout.extend.js"></script>
	<script type="text/javascript" src="${path}/resource/js/system/declareparameter/DateUtils.js"></script>
	<script type="text/javascript" src="${path}/resource/js/common/date/time-date.js?randomId=<%=Math.random()%>"></script>
	<script type="text/javascript">
        var path = "${path}";
        var params=new Object();
		
        $(function(){
            $('#cc').layout('hidden',"west");

            setAjaxLoading();
//            $("#sel_ssq_nd").combobox({
//                required: false,
//                editable: false,
//                panelHeight: 200,
//                valueField: "code",
//                textField: "name",
//                onSelect: function(record) {
//                    calculateSsq();
//                }
//            });

            $("#sel_ssq_xh").combobox({
                required: false,
                editable: false,
                panelHeight: 200,
                valueField: "code",
                textField: "name",
                onSelect: function(record) {
                    calculateSsq();
                }
            });
            
            $("#sel_sbzq").combobox({

                onChange: function (dictCode,o) {

                    changePerd("sel_sbzq", "sel_ssq_nd");
                    
                    var taxCode = $("#sel_sz").combobox("getValue");
                    //属期
                    var period = $("#sel_sbzq").combobox("getValue");
                    $.ajax({
                        type:"POST",
                        url:path+"/tdm/declareParameter/showledgerList?taxCode="+taxCode+'&period='+period,
                        dataType:"json",
                        success:function callback(data){
                            var analyMsg = data.returnObject;
                            if (data.code=="0000") {
                                var dataList =[];
                                $("#ledger").combobox("clear");
                                if(analyMsg.length>0){
                                    for(var i=0;i<analyMsg.length;i++){
                                        dataList.push({"value": analyMsg[i].dictCode,"text":analyMsg[i].dictName});
                                    }
                                    $("#ledger").combobox("loadData",dataList);

                                    if(analyMsg.length==1){
                                        $('#ledger').combobox('setValue', analyMsg[0].dictCode);
                                    }
                                }
                            } else {
                                $.messager.alert('操作提示',data.msg,'info',function(){});
                            }
                        }
                    });
                    refresh_ssq_xh(dictCode);
                }

            });
			

            $("#sel_sz").combobox({

                onChange: function (dictCode,o) {
                    var param = new Object();
                    param.dictCode=dictCode;
                    $.ajax({
                        type : "POST",
                        url : path+'/tdm/declareParameter/queryDeclarePeriod',
                        dataType : "json",
                        data: param,
                        success : function callback(data) {
                            if (data.code == '0000') {
                                var rows = data.returnObject;
                                
                                var dict = rows.dictDto;
                                //汇总类型
                                var gatherType = rows.gatherType;
                                //如果汇总类型为02 汇总显示汇总分支结构
                                if(gatherType == '02'){
                                    $('#cc').layout('show',"west");
                                    
                                }else{
                                    $('#cc').layout('hidden',"west");
                                    var object=new Object();
                                    object.orgCode =${orgCode};
                                    object.orgName=$("#orgName").val();
                                    var array = [];// 创建数组
                                    array.push(object); // 添加到最后
									initGrid(array);
								}
                                var dataList =[];
                                if(dict.length>0){
                                    for(var i=0;i<dict.length;i++){
                                        dataList.push({"value": dict[i].dictCode,"text":dict[i].dictName});
                                    }
                                    $("#sel_sbzq").combobox("clear");

                                    $("#sel_sbzq").combobox("loadData",dataList);
                                    if(dict.length==1){
                                        $('#sel_sbzq').combobox('setValue', dict[0].dictCode);
                                    }
								}
                            } 
                        },
                        error : function(data, textstatus) {
                            $.messager.alert('错误', data.responseText, 'error',function(){});
                        }
                    });
                }

            });
            
        });
		


        
        function clearForm() {
            $("#sel_ssq_nd").combobox("setValue", "");
            $("#sel_ssq_xh").combobox("setValue", "");
			
            
            $("#txt_ssq_q").val("");
            $("#txt_ssq_z").val("");

            $("#txt_sbrq_q").val("");
            $("#txt_sbrq_z").val("");
        }

       
        //保存增加的数据
        function submit_Add(){
            if (!checkInput()) {
                return;
            }

            var url = '${path}/tdm/declareParameter/saveAllMessage';
            var param = getValue();

            $.ajax({
                type : "POST",
                url : url,
                dataType : "json",
                data: param,
                success : function callback(data) {
                    if (data.code != '0000') {
                        $.messager.alert('错误', data.msg, 'error',function() {});
                    } else {
                        $.messager.alert("提示", "添加成功。", "info",function(){
//                            parent.btnQuery();
                            setTimeout("closeLayer()", 100);//设置延时，防止IE下出现异常
                        });
                    }
                },
                error : function(data, textstatus) {
                    $.messager.alert('错误', data.responseText, 'error',function(){});
                }
            });
        }

     

   
        
        
        /**
        * @method      [addTaxpayer]
        * @description [增加纳税主体]
        * @author      [yangruidong]
        * @version     1.0
        * @param       
        * @return      
        * @exception   
        * @date         
        */
        function addTaxpayer() {


      
            //禁止修改
//            $("#sel_sbzq").combobox("disable");
//            $('#sel_sz').combobox('disable');
//            $("#sel_ssq_nd").combobox("disable");
//            $("#sel_ssq_xh").combobox("disable");
//            $("#txt_ssq_q").attr("disabled", "disabled");
//            $("#txt_ssq_z").attr("disabled", "disabled");
            
            var cdmk = new refTree('taxpayer','纳税主体树');
            var object = new Object();
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var node ="";
            var nodes ="";
            //纳税主体Id
            var taxPayerId="";
            if(treeObj!=null){
                //获取所有节点信息
                 node = treeObj.getNodes();
                 nodes = treeObj.transformToArray(node);
                var length = nodes.length-1;
                for(var i=0; i<nodes.length; i++) {
                    if (i < length) {
                        taxPayerId += nodes[i].id + ',';
                    } else {
                        taxPayerId += nodes[i].id;
                    }
                }
			}
            var taxCode =$("#sel_sz").combobox("getValue");
            var period =$("#sel_sbzq").combobox("getValue");
			
            cdmk.url = path+'/tdm/declareParameter/queryTaxpayerData?taxPayerId='+taxPayerId+"&taxCode="+taxCode+"&period="+period;
            cdmk.parameters = object;
            cdmk.callBack = batchAdd;
            cdmk.singleLoad = 0;
            cdmk.selectedMulti = 1;
            cdmk.init();
            cdmk.openRef();
        }
        
        
        function batchAdd(){
            var zTree = $.fn.zTree.getZTreeObj('taxpayer');
			
            var checkNodes = zTree.getCheckedNodes(true);
            if(checkNodes.length==0){
				$.messager.alert('操作提示','请选择至少一个主体','info',function(){});
                return;
            }
            
            //获取根节点
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            
            if(treeObj!=null){
                var node = treeObj.getNodeByParam('pId', 0);
                var pTaxpayerCode = "";
                if(node!=null){
                    pTaxpayerCode = node.code;
                }
			}
            
            var length = checkNodes.length-1;
            //选择数据得code 也就是纳税主体codes
            var codes="";
            for(var i=0; i<checkNodes.length; i++){
                if(i<length){
                    codes += checkNodes[i].code+',';
                }else{
                    codes += checkNodes[i].code;
                }
            }
            //税种
            var taxCode = $("#sel_sz").combobox("getValue");
            //周期
            var tdPerdCode = $("#sel_sbzq").combobox("getValue");
            //属期
            var taxPerd = getTaxPerd();
            var ssq_q = $("#txt_ssq_q").val();
            var ssq_z = $("#txt_ssq_z").val();

            var array =  ssq_q.split("-");
            var array2 =  ssq_z.split("-");
            var  taxperdFrom = DateUtils.format(new Date(array[0], array[1]-1, array[2]), 'yyyy-MM-dd');
            var  taxperdTo =DateUtils.format( new Date(array2[0], array2[1]-1, array2[2]), 'yyyy-MM-dd');
//
//			var  taxperdFrom = new Date(array[0], array[1]-1, array[2]);
//            var  taxperdTo = new Date(array2[0var ledger = ], array2[1]-1, array2[2]);
            var ledger = $("#ledger").combobox("getValue");
            var url =path+"/tdm/declareParameter/saveTaxpayer?codes="+codes+"&pTaxpayerCode="+pTaxpayerCode+"&taxCode="+taxCode+"&tdPerdCode="+tdPerdCode+"&taxPerd="+taxPerd+"&ledger="+ledger;
//           var url =path+"/tdm/declareParameter/saveTaxpayer?codes="+codes+"&pTaxpayerCode="+pTaxpayerCode+"&taxCode="+taxCode+"&tdPerdCode="+tdPerdCode+"&taxPerd="+taxPerd+"&startDate="+taxperdFrom+"&endDate="+taxperdTo;
            $.ajax({
                type:"POST",
                url:url,
                //data:params,
                dataType:"json",
                success:function callback(data){
                    var analyMsg = data.returnObject;
                    if (data.code=="0000") {
                        $.fn.zTree.init($("#treeDemo"), setting, analyMsg.taxList);
                        //默认第一个节点选中
                        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                        var node = zTree.getNodeByParam('pId', 0);
                        zTree.selectNode(node);
                        zTree.setting.callback.onClick(null, null, node);
                        
                    } else {
                        $.messager.alert('信息',data.msg,'info',function(){});
                    }
                }
            });
        }

        var setting = {
            callback: {
                onClick: zTreeOnClick

            },
            data: {
                key: {
                    name: "name",
                    title: ""
                },
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: "0"
                }
            }
        };



        function zTreeOnClick(event, treeId, treeNode) {
            var orgCode=treeNode.id;
            params.orgCode=orgCode;
            params.taxPayerCode = treeNode.code;
            params.taxCode =$("#sel_sz").combobox("getValue");
            params.tdPerdCode =$("#sel_sbzq").combobox("getValue");
            params.taxPerd =getTaxPerd();
            params.cbid = "-1";
            $.ajax({
                type:"POST",
                url:path+"/tdm/declareParameter/showOriginAndLedger",
                data:params,
                dataType:"json",
                success:function callback(data){
                    var analyMsg = data.returnObject;
                    if (data.code=="0000") {
                        initGrid(analyMsg.orgDto);
//                        $("#tt").treegrid('loadData', []);
//                        $("#tt").treegrid('loadData', analyMsg.orgDto);
                    } else {
                        $.messager.alert('信息',data.msg,'info',function(){});
                    }
                }
            });
        }
        /**
        * @method      [initGrid]
        * @description [根据纳税主体查询机构得展示]
        * @author      [yangruidong]
        * @version     1.0
        * @param       
        * @return      
        * @exception   
        * @date         
        */
        function initGrid(analyMsg) {
            $('#tt').treegrid({
                url : '',
                idField : 'id',
                treeField : 'id',
                toolbar : $('#tb'),
                columns : [ [ {
                    field: "id", title: "id", align: "left",sortable: false,hidden:true, resizable: true, width: 80
                }, {
                    field: "orgCode", title: "机构编码", align: "left", sortable: false, resizable: true, width: 80
                }, {
                    field: "orgName", title:"机构名称",align: "left", sortable: false, resizable: true, width: 280
                } 
                 ] ]
            });
            $("#tt").treegrid('loadData', analyMsg);
            $("#tt").treegrid('collapseAll');
        }
        /**
        * @method      [delOrg]
        * @description [删除纳税主体]
        * @author      [yangruidong]
        * @version     1.0
        * @param       
        * @return      
        * @exception   
        * @date         
        */
        function delTaxpayer() {
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var node = treeObj.getSelectedNodes();
            var length = node.length-1;

            if (node.length <=0) {
                $.messager.alert("信息", "请选择一个纳税主体！", "info");
                return;
            }
            $.messager.alert("信息", "是否该纳税主体删除？","confirm", function() {
                    
                    var taxPayerCode = "";
                    //先移除选择节点，数据在后台处理
                    for(var i=0;i<node.length;i++){
                        //移除
                        treeObj.removeNode(node[i]);
                        if(i<length){
                            taxPayerCode += node[i].code+',';
                        }else{
                            taxPayerCode += node[i].code;
                        }
                    }

                    params.taxCode =$("#sel_sz").combobox("getValue");
                    params.tdPerdCode =$("#sel_sbzq").combobox("getValue");
					params.taxPayerCode=taxPayerCode;
                    params.cbid = "-1";
                    params.flag ="1";
                    $.ajax({
                        type:"POST",
                        url:path+"/tdm/declareParameter/deleteTaxpayer",
                        data:params,
                        dataType:"json",
                        success:function callback(data){
                            var analyMsg = data.returnObject;
                            if (data.code=="0000") {
                                //清空机构列表
                                $('#tt').treegrid('loadData', []);
                            } else {
                                $.messager.alert('信息',data.msg,'info',function(){});
                            }
                        }
                    });
           
            });
        }

       
        
        /**
        * @method      [delOrg]
        * @description [删除数据来源]
        * @author      [yangruidong]
        * @version     1.0
        * @param       
        * @return      
        * @exception   
        * @date         
        */
        function delOrg() {

            var deletNode = $('#tt').treegrid('getSelected');

            if(deletNode==null){
                $.messager.alert("信息", "请选择数据来源机构", "info");
                return;
            }

            $.messager.alert('确认', '您确定要删除此选中行吗?',"confirm", function () {

                    $('#tt').treegrid('remove',deletNode.orgCode);

                    var obj_node = $('#tt').treegrid('getRoots');

                    var length = obj_node.length-1;
                    var orgCodes="";
                    for(var i=0; i<obj_node.length; i++) {
                        if (i < length) {
                            orgCodes += obj_node[i].orgCode + ',';
                        } else {
                            orgCodes += obj_node[i].orgCode;
                        }
                    }
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                    var nodes = treeObj.getSelectedNodes();
                    //新增数据来源，删除数据来源调用方法一样
                    params.taxpayerId = orgCodes;
                    params.taxCode =$("#sel_sz").combobox("getValue");
                    params.tdPerdCode =$("#sel_sbzq").combobox("getValue");
                    params.taxPayerCode=nodes[0].code;
                    params.taxPerd =getTaxPerd();
                    params.cbid = "-1";
                    $.ajax({
                        type:"POST",
                        url:path+"/tdm/declareParameter/deleteOrAddOrgCodes",
                        data:params,
                        dataType:"json",
                        success:function callback(data){
                            var analyMsg = data.returnObject;
                            if (data.code=="0000") {

                            } else {

                            }
                        }
                    });

            });

        }
    

        function callback_selectLedger(data){
            params = data;
        }
      
		
		function checkTaxPayer(){
//            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");\
            var treeObj = $("#sel_sz").combobox("getValue");
            if(treeObj==null){
                return false;
			}else{
                return true;
			}
		}
	</script>
</head>
<body  style="margin:0px; padding:0px;" >

<div class="easyui-layout" fit="true" >

	<input type="hidden" value="${orgName}" id="orgName">
	
	<div data-options="region:'north',title:'申报参数',split:false,border: true" style="padding: 5px; height: 120px; background: none repeat scroll 0 0 #EFEFEF;">
		<form id="sbcsForm" method="post" >
			<table align="center" cellspacing="1">
				<colgroup>
					<col style="width:150px;"></col>
					<col style="width:250px;"></col>
					<col style="width:150px;"></col>
					<col style="width:250px;"></col>
				</colgroup>
				<tr>
					<td align="right">
						纳税主体：
					</td>
					<td align="left">

						<select class="easyui-combobox" id="taxPayers" name="sel_sz" style="width:250px;" data-options="editable: false">
							<%--<option value="">--请选择--</option>--%>
							<c:forEach var="item"  items="${taxPayers}">
								<option value="${item.id}">${item.name}</option>
							</c:forEach>
						</select>
					</td>

					<td align="right">
						税（费）种：
					</td>
					<td align="left">

						<select class="easyui-combobox" id="sel_sz" name="sel_sz" style="width:250px;" data-options="editable: false">
							<option value="">--请选择--</option>
							<c:forEach var="item"  items="${taxClassList}">
								<option value="${item.dictCode}">${item.dictName}</option>
							</c:forEach>
						</select>
					</td>

				</tr>
				<tr>
					<td align="right">
						申报周期：
					</td>
					<td align="left">

						<select class="easyui-combobox" id="sel_sbzq" name="sel_sbzq" style="width:250px;"  onchange="initData(this.value)" data-options="editable: false">
							<option value="">--请选择--</option>
						</select>
					</td>
					<%--<td align="right">--%>
						<%--申报属期：--%>
					<%--</td>--%>
					<%--<td align="left">--%>
						<%--<select id="sel_ssq_nd" name="sel_ssq_nd" class="easyui-combobox" style="width:104px;" data-options="editable: false, required: false">--%>
							<%--<option value="">--请选择--</option>--%>
						<%--</select>年--%>
						<%--<select id="sel_ssq_xh" name="sel_ssq_xh" class="easyui-combobox" style="width:104px;" data-options="editable: false, required: false">--%>
							<%--<option value="">--请选择--</option>--%>
						<%--</select>--%>
						<%--<span id="span_ssq_xh">期</span>--%>
					<%--</td>--%>
					<td align="right">申报属期：</td>
					<td align="left">
						<input id="sel_ssq_nd" name="sel_ssq_nd" class="Wdate"  style="width: 140px;" placeholder="请先选择申报周期" onChange="yearChange()" data-options="editable: false">
						<select id="sel_ssq_xh" name="sel_ssq_xh" class="easyui-combobox" style="width:80px;" data-options="editable: false, required: false">
							<option value="">--请选择--</option>
						</select>
						<span id="span_ssq_xh">期</span>
					</td>

				</tr>
				<tr>
					<td align="right">
						所属期起止：
					</td>
					<td align="left">
						<input type="text" id="txt_ssq_q" name="txt_ssq_q" class="Wdate" style="width: 100px;" onFocus="WdatePicker()" data-options="editable: false"> - <input type="text" id="txt_ssq_z" name="txt_ssq_z" class="Wdate" style="width: 100px;" onFocus="WdatePicker()" data-options="editable: false">
					</td>

					<td align="right">
						台账设置：
					</td>
					<td align="left">

						<select class="easyui-combobox" id="ledger" name="ledger" style="width:250px;"  onchange="initData(this.value)" data-options="editable: false">
							<option value="-1">--请选择--</option>
						</select>
					</td>

				</tr>
			</table>
		</form>
	</div>





	<div region="center" style="padding: 5px;"  border="false">
		<div id="cc" class="easyui-layout"  fit="true"  border="false">
			<div data-options="region:'west',split:false"  border="true" noheader="true" split="false"  title="汇总分支机构" style="width:50%;">
				<div class="easyui-layout" fit="true"  border="false">
					<div region="center" class ="panel-title" title="" border="false" >

						<div class="panel-header">
							<span>汇总分支机构</span>&nbsp;&nbsp;&nbsp;
							<a id="btn_addNode_org" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="false" onclick="addTaxpayer()">新增</a>

							<a id="btn_delNode_org2" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="false" onclick="delTaxpayer()">删除</a>
						</div>
						<div region="center" title="" border="false" noheader="true"
							 style="padding: 5px 0 0 0;overflow-y:hidden;">
							<div>
								<ul id="treeDemo" class="ztree" style="width: 90%; height: 90%;">

								</ul>
							</div>
						</div>
					</div>

				</div>
			</div>
			<div data-options="region:'center',title:'数据来源'" border="true" noheader="true" split="false"  style="width:50%;">
				<div class="easyui-layout" fit="true"  border="false">
					<div region="center" class ="panel-title" title="数据来源" border="false" style="height:50%;">

						<div class="panel-header">
							<span>数据来源</span>&nbsp;&nbsp;&nbsp;
							<a id="btn_addNode_org" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="false" onclick="addOrg()">新增</a>
							<a id="btn_delNode_org2" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="false" onclick="delOrg()">删除</a>
						</div>
						<div region="center" title="" border="false" noheader="true"
							 style="padding: 5px 0 0 0;overflow-y:hidden;">
							<table id="tt" title="" border="false"></table>
						</div>
					</div>

				</div>
				
			</div>
		</div>
	</div>
	<div data-options="region:'south',title:'',split:false,border: false" style="height:55px;">
		<div align="center" style="margin-top: 15px;">
			<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="submit_Add()">确定</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="closeBtn();">取消</a>
		</div>
	</div>
</div>
</div>
</div>

</body>
</html>
